package com.siyu.rehearsal.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.siyu.basedata.plan.domain.Plan;
import com.siyu.basedata.plan.service.IPlanService;
import com.siyu.basedata.wrprsrhych.domain.WrpRsrHych;
import com.siyu.basedata.wrprsrhych.service.IWrpRsrHychService;
import com.siyu.basedata.xajresult.domain.XajResult;
import com.siyu.basedata.xajresult.service.IXajResultService;;
import com.siyu.common.core.domain.AjaxPageResult;
import com.siyu.rehearsal.previewaffectv.domain.PreviewAffectV;
import com.siyu.rehearsal.previewaffectv.mapper.PreviewAffectVMapper;
import com.siyu.rehearsal.previewaffectv.service.IPreviewAffectVService;
import com.siyu.rehearsal.previewnd.domain.PreviewNd;
import com.siyu.rehearsal.previewnd.service.IPreviewNdService;

import com.siyu.rehearsal.previewresult.domain.PreviewResult;
import com.siyu.rehearsal.previewresult.service.IPreviewResultService;
import com.siyu.rehearsal.previewriska.domain.PreviewRiskA;
import com.siyu.rehearsal.previewriska.service.IPreviewRiskAService;
import com.siyu.rehearsal.previewsresult.domain.PreviewSresult;
import com.siyu.rehearsal.previewsresult.service.IPreviewSresultService;
import com.siyu.rehearsal.service.PlanHomepageService;
import com.siyu.rehearsal.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.*;

@Service
public class PlanHomepageServiceImpl implements PlanHomepageService {

    @Autowired
    private IPreviewAffectVService previewAffectVService;

    @Autowired
    private PreviewAffectVMapper previewAffectVMapper;


    @Autowired
    private IXajResultService xajResultService;

    @Autowired
    private IPreviewNdService previewNdService;

    @Autowired
    private IPreviewSresultService previewSresultService;

    @Autowired
    private IPreviewResultService previewResultService;

    @Autowired
    private IPreviewRiskAService previewRiskAService;

    @Autowired
    private IWrpRsrHychService wrpRsrHychService;

    @Autowired
    private IPlanService planService;

    @Override
    public AjaxPageResult getPreviewAffectV(String id, Integer pageSize, Integer pageNum) {
        Page<PreviewAffectV> page = new Page<>(pageNum, pageSize);
        QueryWrapper<PreviewAffectV> planQueryWrapper = new QueryWrapper<>();
        planQueryWrapper.select("village_name", "district", "l_official", "inp")
                .eq("plan_id", id);

        // 执行分页查询
        IPage<PreviewAffectV> result = previewAffectVMapper.selectPage(page, planQueryWrapper);

        // 获取分页数据
        List<PreviewAffectV> records = result.getRecords(); // 分页后的数据列表
        long total = result.getTotal(); // 总记录数
        // 封装分页数据到自定义VO类或直接使用Map等，这里以自定义VO为例
        List<PreviewAffectVVo> respList = new ArrayList<>();
        for (PreviewAffectV previewAffectV : result.getRecords()) {
            PreviewAffectVVo respVO = new PreviewAffectVVo();
            // 将Plan实体转换为VO对象，根据实际需求映射字段
            respVO.setDistrict(previewAffectV.getDistrict());
            respVO.setInp(previewAffectV.getInp());
            respVO.setLOfficial(previewAffectV.getLOfficial());
            respVO.setVillageName(previewAffectV.getVillageName());
            respList.add(respVO);
        }
        // 构建响应对象
        AjaxPageResult ajaxPageResult = new AjaxPageResult();
        ajaxPageResult.setRecords(respList); // 设置数据列表
        ajaxPageResult.setTotal(result.getTotal()); // 设置总记录数

        return ajaxPageResult;
    }

    @Override
    public Object getReservoir(String id) {


        QueryWrapper<PreviewResult> previewResultQueryWrapper = new QueryWrapper<>();
        previewResultQueryWrapper.select("tm", "in_qt", "out_qt", "final_level")
                .eq("plan_id", id);
        List<PreviewResult> previewResults = previewResultService.list(previewResultQueryWrapper);

        QueryWrapper<PreviewRiskA> previewRiskAQueryWrapper = new QueryWrapper<>();
        previewRiskAQueryWrapper.select("s_depth","sm_depth")
                .eq("plan_id", id);
        PreviewRiskA previewRiskA = previewRiskAService.getOne(previewRiskAQueryWrapper);

        Plan plan = planService.getById(id);

        // 查询水库特征信息
        WrpRsrHych wrpRsrHych = wrpRsrHychService.lambdaQuery().eq(WrpRsrHych::getRscd, plan.getRscd()).one();
        Double warnZ = wrpRsrHych.getMjflsscnwl(); // 汛限水位

        HashMap<String, Object> map = new HashMap<>();
        map.put("previewResults", previewResults);
        map.put("sDepth",previewRiskA.getSDepth());
        map.put("smDepth",previewRiskA.getSmDepth());
        map.put("mjflsscnwl",warnZ);
        return map;
    }

    @Override
    public ArrayList<PreviewNdVo> getRainfall(String id) {
        //根据id查询预演方案详细参数
        QueryWrapper<PreviewNd> previewNdQueryWrapper = new QueryWrapper<>();
        previewNdQueryWrapper.select("tm", "drp")
                .eq("plan_id", id);
        List<PreviewNd> previewNds = previewNdService.list(previewNdQueryWrapper);

        ArrayList<PreviewNdVo> previewNdVos = new ArrayList<>();
        Double sumdrp = 0.0;
        for (int i = 0; i < previewNds.size(); i++) {
            PreviewNdVo previewNdVo = new PreviewNdVo();
            Double drp = previewNds.get(i).getDrp();
            Date tm = previewNds.get(i).getTm();
            sumdrp += previewNds.get(i).getDrp();
            previewNdVo.setDrp(drp);
            previewNdVo.setSumdrp(sumdrp);
            previewNdVo.setTm(tm);
            previewNdVos.add(previewNdVo);
        }
        return previewNdVos;
    }

    @Override
    public List<String> getView(String id,Integer dataType) {
        QueryWrapper<PreviewSresult> previewSresultQueryWrapper = new QueryWrapper<>();
        previewSresultQueryWrapper.select("geojson")
                .eq("plan_id",id);
        PreviewSresult previewSresult = previewSresultService.getOne(previewSresultQueryWrapper);
        List<String> list = Convert.toList(String.class, previewSresult.getGeojson());

        ArrayList<String> geojsons = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            String s = list.get(i);
            if (dataType == 0){
                if (s.contains("max_wDepth")){
                    continue;
                }else {
                    geojsons.add(s);
                }
            }else {
                if (!s.contains("max_wDepth")){
                    continue;
                }else {
                    geojsons.add(s);
                }
            }

        }

        return geojsons;
    }
}
